//
//  NavBar.m
//  SulitLoan
//
//  Created by 虞嘉伟 on 2025/2/24.
//  
  

#import "NavBar.h"
#import <YYKit.h>
#import <Masonry.h>


@implementation NavBar

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.bar];
        [self.bar addSubview:self.backButton];
        [self.bar addSubview:self.titleLabel];
        [self.bar mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self);
            make.height.mas_equalTo(44);
        }];
        [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.bar).offset(15);
            make.centerY.equalTo(self.bar);
            make.width.height.mas_equalTo(30);
        }];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self.bar);
            make.left.equalTo(self.backButton.mas_right);
            make.right.equalTo(self.bar);
        }];
        self.theme = NavBarThemeLight;
    }
    return self;
}

/**
 @property (nonatomic, strong) UIView *bar;
 @property (nonatomic, strong) UIButton *backButton;
 @property (nonatomic, strong) UILabel *titleLabel;
 */

- (void)setTheme:(NavBarTheme)theme {
    _theme = theme;
    if (theme == NavBarThemeDark) {
        self.backgroundColor = UIColorHex(#1B1B1B);
        self.titleLabel.textColor = UIColor.whiteColor;
    } else {
        self.backgroundColor = UIColor.whiteColor;
        self.titleLabel.textColor = UIColorHex(#1B1B1B);
    }
}
- (UIView *)bar {
    if (!_bar) {
        _bar = [UIView new];
    }
    return _bar;
}
- (UIButton *)backButton {
    if (!_backButton) {
        _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backButton setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
        @weakify(self);
        [_backButton jk_addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
            @strongify(self);
            if (self.backBlock) {
                self.backBlock();
            }
        }];
    }
    return _backButton;
}
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}
@end
